var config = require("../../config");
//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    userInfo: {},
  me2other:0,
  other2me:0,
  my_books:0,
  loaned:0,//借出的书
  borrowed:0,//借入的书
  },

  onLoad: function () {
    console.log('onLoad')
    var that = this;
    that.setData({
      userInfo: app.globalData.userInfo
    })
  },
  onShow: function () {
    // 每次显示，更新一下用户最新信息。虽然有点笨，但是好像比较可靠
    var that = this;
    var user_id = app.globalData.userInfo.id;
    if (user_id) {
      wx.request({
        url: config.urls.user_one,
        data: {
          id: user_id
        },
        success: function (res) {
          console.log(res);
          if (res.data.success) {
            app.globalData.userInfo = res.data.user;
            that.setData({
              userInfo: res.data.user
            });
          }
        }
      });
      
      wx.request({
        url: config.urls.book_status_counts,
        data: {
          id: user_id
        },
        success: function (res) {
          console.log(res);
          if (res.data.success) {
            that.setData({
              me2other: res.data.applies_by_me,
              other2me: res.data.applies_to_me,
              my_books: res.data.books_my,
              loaned: res.data.books_i_borrowed_out,
              borrowed: res.data.books_i_borrowed_in
            });
          }
        }
      });
    }
  },

  onShelf: function (e) {
    var wxid = this.data.userInfo.wxid;
    // if (wxid != '') {
      wx.navigateTo({
        url: '/pages/onshelf/onshelf'
      })
    // }
    // else {
    //   wx.showToast({
    //     title: '上架新书前，请填写你的微信号',
    //     icon: 'success',
    //     duration: 1500
    //   })
      return;
    // }
  },

  modifyWxid: function (e) {
    wx.navigateTo({
      url: '/pages/modifywxid/modifywxid'
    })
  },

  modifySay: function (e) {
    wx.navigateTo({
      url: '/pages/modifysay/modifysay'
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '我的书架',
      path: that.route,
      success: function (res) {
        // 转发成功
        console.log(res.target)
      },
      fail: function (res) {
        // 转发失败
        console.log(res.target)
      }
    }
  },

  userInfoHandler: function (e) {
    var that = this;
    var detail = e.detail;
    console.log('in userInfoHandler', e);
    var userInfo = detail.userInfo;
    if (userInfo) {
      wx.request({
        url: config.urls.user_new,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          openid: app.globalData.openid,
          nickname: userInfo.nickName,
          avatar: userInfo.avatarUrl,
          city: 1
        },

        success: function (res) {
          if (res.data.success) {
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 1500
            });

            app.globalData.userInfo = res.data.user;
            that.setData({
              userInfo: res.data.user
            });
            wx.setStorageSync('bob_id', res.data.user.id);
          }
          else {
            console.log("User register failed.");
          }
        }
      });
    }
  }
})
